package com.zzxy.spring21.factory;

import com.zzxy.spring21.aspect.MyAspect;
import com.zzxy.spring21.service.IStudentService;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class MyBeanFactory {
    public static IStudentService createStudentServiceProxy(){
        StudentServiceImpl studentService = new StudentServiceImpl();

        MyAspect myAspect = new MyAspect();
        IStudentService studentServiceProxy = (IStudentService) Proxy.newProxyInstance(MyBeanFactory.class.getClassLoader(), studentService.getClass().getInterfaces(), new InvocationHandler() {
            @Override
            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                myAspect.before();


                Object obj = method.invoke(studentService, args);
                myAspect.after();
                return obj;
            }
        });
        return studentServiceProxy;

    }
}
